package com.heima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heima.mapper.SkuMapper;
import com.heima.pojo.Sku;
import com.heima.pojo.dto.SkuDto;
import com.heima.pojo.vo.SkuVo;
import com.heima.service.SkuService;
import com.heima.utils.PageResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;


/**
 * @author 赵超
 */
@Service
public class SkuServiceImpl implements SkuService {
    @Autowired
    private SkuMapper skuMapper;

    @Override
    public Integer add(SkuDto skuDto) {
        Sku sku = new Sku();
        BeanUtils.copyProperties(skuDto, sku);
        sku.setCreateTime(LocalDateTime.now());
        sku.setUpdateTime(LocalDateTime.now());
        return skuMapper.insert(sku);
    }

    @Override
    public PageResult<SkuVo> page(Integer pageIndex, Integer pageSize, String skuName) {
        PageHelper.startPage(pageIndex, pageSize);
        Page<SkuVo> list = skuMapper.page(skuName);

        return new PageResult<>(
                list.getPageNum(),
                list.getPageSize(),
                list.getPages(),
                list.getTotal(),
                list
        );
    }

    @Override
    public Integer update(Long skuId, SkuDto skuDto) {

        return skuMapper.updateBySkuId(skuId, skuDto);
    }
}
